package com.xt.base.model.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.xt.base.dao.hibernate.code.IAutoCreateCode;
import com.xt.base.dao.hibernate.system.SystemDaoTool;
import com.xt.base.model.base.Organization;
import com.xt.base.model.base.SpelledField;
import com.xt.base.model.dictionary.ApplicationType;
import com.xt.base.model.dictionary.DesktopItem;
import com.xt.base.model.dictionary.Function;
import com.xt.base.model.dictionary.FunctionGroup;
import com.xt.base.pojo.BasePOJO;


/**
 * 操作员(用户) <br>
 * 
 * <p>
 * <a href="Station.java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @version $Revision: $<br>
 *          $Id: $
 * @hibernate.class table="Users" batch-size="200" dynamic-update="true"
 *                  dynamic-insert="true"
 */
public class User extends BasePOJO implements IAutoCreateCode {

	private static final long serialVersionUID = 6971766952472326873L;


	/** 用户登陆名 */
	private String username;

	/** 用户锁时的登陆密码 */
	private String password;

	/** 不使用用户锁时的登陆密码 */
	private String noEncryptLockpassword;

	/** 用户锁id */
	private String lockedId;
	
	private String simNum;
	
	private boolean lock = false;
	
	/** 中文名称 */
	private SpelledField zhname;
	/** 手机号码 */
	private String telNumber;
	/** 组织结构 */
	private Organization org;
	/** 用户对应的所有角色 */
	private Set<Role> roles;
	
	/** 进入时间 */
	private Date enterDate;

	/** 离开时间 */
	private Date leaveDate;
	
	/**撤销原因*/
    private String leavingReason;
	
	/**
	 * @hibernate.property column="LockedId"
	 * @return 用户锁id
	 */
	public String getLockedId() {
		return lockedId;
	}

	public void setLockedId(String lockedId) {
		this.lockedId = lockedId;
	}

	/**
	 * @hibernate.property column="NoEncryptLockpassword" length="1000"
	 * @return 不使用用户锁时的登陆密码
	 */
	public String getNoEncryptLockpassword() {
		return noEncryptLockpassword;
	}

	public void setNoEncryptLockpassword(String noEncryptLockpassword) {
		this.noEncryptLockpassword = noEncryptLockpassword;
	}

	/**
	 * @hibernate.property column="Password" length="1000"
	 * @return 使用用户锁时的登陆密码
	 */
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @hibernate.property column="Username" not-null="true" unique="true"
	 *                     length="15"
	 * @return 用户登陆名
	 */
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @hibernate.property column = "SimNum"
	 * @return SIM卡号
	 */
	public String getSimNum() {
		return simNum;
	}

	public void setSimNum(String simNum) {
		this.simNum = simNum;
	}
	/**
	 * @hibernate.property column = "Locked"
	 * @return 是否锁定当前帐号
	 */
	public boolean isLock() {
		return lock;
	}

	public void setLock(boolean lock) {
		this.lock = lock;
	}
	/**
	 * @hibernate.component
	 * @return 
	 */
	public SpelledField getZhname() {
		return zhname;
	}

	public void setZhname(SpelledField zhname) {
		this.zhname = zhname;
	}
	/**
	 * @hibernate.many-to-one column = "Org"
	 * @return 
	 */
	public Organization getOrg() {
		return org;
	}

	public void setOrg(Organization org) {
		this.org = org;
	}
	/**
	 * @hibernate.set table="UserRoles"  inverse="false" cascade="save-update" lazy="true"
	 * @hibernate.collection-key column="UserID"
	 * @hibernate.collection-many-to-many 
	 * 			class="com.xt.base.model.system.Role" column="RoleID"
 	 * @return 全部角色
	 */
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	/**
	 * @hibernate.property column = "EnterDate"
	 * @return 
	 */
	public Date getEnterDate() {
		return enterDate;
	}

	public void setEnterDate(Date enterDate) {
		this.enterDate = enterDate;
	}
	/**
	 * @hibernate.property column = "LeaveDate"
	 * @return 
	 */
	public Date getLeaveDate() {
		return leaveDate;
	}

	public void setLeaveDate(Date leaveDate) {
		this.leaveDate = leaveDate;
	}
	/**
	 * @hibernate.property column = "LeavingReason"
	 * @return 
	 */
	public String getLeavingReason() {
		return leavingReason;
	}

	public void setLeavingReason(String leavingReason) {
		this.leavingReason = leavingReason;
	}
	/**
	 * @hibernate.property column = "TelNumber"
	 * @return 
	 */
	public String getTelNumber() {
		return telNumber;
	}

	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}

	private List<Function> functions;

	private List<FunctionGroup> groups;

	private List<ApplicationType> apps;

	private List<DesktopItem> desktopItems;


	/**
	 * @return 用户的全部操作
	 */
	public List<Function> getFunctions() {
		if (functions == null)
			functions = SystemDaoTool.getInstance().getFunctionsByUserCode(getCode());
		return functions;
	}

	public List<DesktopItem> getDesktopItems() {
		if (desktopItems == null) {
			desktopItems = SystemDaoTool.getInstance().getDesktopItemsByUserCode(getCode());
		}
		return desktopItems;
	}

	/**
	 * @return 用户的全部功能组
	 */
	public List<FunctionGroup> getFunctionGroups() {
		if (groups == null)
			groups = SystemDaoTool.getInstance().getFunctionGroupssByUserCode(getCode());
		return groups;
	}

	/**
	 * @return 用户的全部应用
	 */
	public List<ApplicationType> getApps() {
		if (apps == null){
			Set<ApplicationType> gset = new HashSet<ApplicationType>();
			for (FunctionGroup group : getFunctionGroups())
				gset.add(group.getAppType());
			apps = new ArrayList<ApplicationType>(gset);
		}
		return apps;
	}

	/**
	 * @return 用户在某应用上的的全部角色
	 * @param 应用
	 */
	public List<Role> getApplicationRoles(Application app) {
		return SystemDaoTool.getInstance().getRolesByUserCodeAndAppCode(getCode(), app.getCode());
	}

	public void initialize() {
		if(!initialized){
			initialized = true;
			if(organName == null)
				organName = org.getName().getField();
			if(functions == null)
				functions = SystemDaoTool.getInstance().getFunctionsByUserCode(getCode());
			if(desktopItems == null)
				desktopItems = SystemDaoTool.getInstance().getDesktopItemsByUserCode(getCode());
			getFunctionGroups();
		}
	}

	private String organName;
	private boolean initialized = false;
	
	public String getOrganName(){
		return organName;
	}


}
